package br.com.ro.security;

import java.io.IOException;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;

import br.com.ro.util.Constantes;

public class Security implements PhaseListener {

	private static final long serialVersionUID = 1L;

	@Override
	public void afterPhase(PhaseEvent event) {
		FacesContext fc = event.getFacesContext();
		boolean validar = fc.getViewRoot().getViewId().lastIndexOf("login") > -1 ? true : false;
		if (!validar) {
			ExternalContext context = fc.getExternalContext();
			if (Constantes.getUsuarioId() == null) {
				try {
					context.redirect("login.sco");
				} catch (IOException e) {
				}
			}
		}

	}

	@Override
	public void beforePhase(PhaseEvent arg0) {
	}

	@Override
	public PhaseId getPhaseId() {
		return PhaseId.RESTORE_VIEW;
	}

}
